import java.security.MessageDigest;
import java.sql.*;
import java.text.MessageFormat;
import java.util.Scanner;

public class Text_DriverManager {
    public static void main(String[] args) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            // 加载数据库厂商提供的驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 获取数据库连接
            connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/java113?characterEncoding=utf8" +
                    "&allowPublicKeyRetrieval=true&useSSL=false", "root", "123456");
            // 创建一个Statement对象来SQL语句并返回执行结果的对象
            statement = connection.createStatement();
            // 获取要查询的人
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要查询的姓名");
            String name = sc.next();
            // 设置查询语句
            String sql = "select student_id, sn, name, mail, class_id from student where name = '" + name +"'";
            resultSet = statement.executeQuery(sql);
            // 获取学生信息
            while (resultSet.next()){
                long id = resultSet.getLong(1);
                String sn = resultSet.getString(2);
                String sName = resultSet.getString(3);
                String mail = resultSet.getString(4);
                long cId = resultSet.getLong(5);
                System.out.println(MessageFormat.format("学生编号={0},学生学号={1},学生姓名={2},学生邮箱={3},班级名={4}",id,sn,sName,mail,cId));
            }
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
